package com.example.springbootthreaddemo;

import com.example.springbootthreaddemo.persistence.User;
import com.example.springbootthreaddemo.service.IUserService;
import com.example.springbootthreaddemo.service.SmsClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @Author: jixiaotian.ch@qq.com
 * @Date : 2020/8/25
 */
@RestController
public class UserController {
    @Autowired
    IUserService userService;

    @Autowired
    SmsClient smsClient;

    @PostMapping("/user")
    public String addUser(User user) {
        long start = System.currentTimeMillis();
        int insert = userService.insert(user);
        long end = System.currentTimeMillis();
        return "SUCCESS:" + (end-start);
    }

    @PostMapping("/sms/user")
    public String sendSms(User user) {
        long start = System.currentTimeMillis();
        int insert = userService.insert(user);

        ExecutorService executorService = Executors.newFixedThreadPool(10);
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                String smsMsg = smsClient.sendSms("15346180653");
            }
        });

        long end  = System.currentTimeMillis();
        return "SUCCESS:" + (end-start);
    }

}
